<template>
	<div>
		<button type="primary" @click="initPrinter">初始化打印</button>
		<button type="primary" @click="closePrinter">关闭打印</button>
		<button type="primary" @click="printerText">打印小票</button>
		<button type="primary" @click="printerLabel">测试打印</button>
		<button type="primary" @click="printLabel">打印标签</button>
	</div>
</template>

<script>
	let printer = null;
	let modal = null;
	let globalEvent = null;
	
	try {
		printer = uni.requireNativePlugin('LcPrinter');
		modal = uni.requireNativePlugin('modal');
		globalEvent = uni.requireNativePlugin('globalEvent');
	} catch (error) {
		console.error('打印机插件加载失败:', error);
	}

	export default {
		data() {
			return {
				isPrinterInitialized: false
			};
		},
		onLoad: function() {
			this.setupPrinterListeners();
		},
		onHide: function() {
			this.removePrinterListeners();
		},
		destroyed: function() {
			this.removePrinterListeners();
		},
		methods: {
			setupPrinterListeners() {
				if (!globalEvent) return;
				
				// 添加打印状态监听
				globalEvent.addEventListener('onPrintCallback', (e) => {
					console.log("打印状态监听:", e);
					if (e.key === 0) {
						uni.showToast({
							title: '打印成功',
							icon: 'success',
							duration: 2000
						});
					} else if (e.key === 3) {
						uni.showToast({
							title: '打印机缺纸',
							icon: 'error',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: '打印异常',
							icon: 'error',
							duration: 2000
						});
					}
				});

				globalEvent.addEventListener('onVersion', (e) => {
					console.log('打印机版本:', e);
					uni.showToast({
						title: '版本: ' + JSON.stringify(e),
						icon: 'none',
						duration: 2000
					});
				});

				globalEvent.addEventListener('getsupportprint', (e) => {
					console.log('支持的打印模块:', e);
					uni.showToast({
						title: '支持模块: ' + JSON.stringify(e),
						icon: 'none',
						duration: 2000
					});
				});
			},
			
			removePrinterListeners() {
				if (!globalEvent) return;
				
				globalEvent.removeEventListener('onPrintCallback');
				globalEvent.removeEventListener('onVersion');
				globalEvent.removeEventListener('getsupportprint');
			},

			async initPrinter() {
				if (!printer) {
					uni.showToast({
						title: '打印机插件未加载',
						icon: 'error',
						duration: 2000
					});
					return;
				}

				try {
					console.log('初始化打印机...');
					await printer.initPrinter({});
					
					// 配置打印机基本设置
					await printer.printEnableMark({ enable: false });
					await printer.setConcentration({ level: 39 });
					await printer.setLineSpacing({ spacing: 1 });
					await printer.getsupportprint();
					
					this.isPrinterInitialized = true;
					uni.showToast({
						title: '打印机初始化成功',
						icon: 'success',
						duration: 2000
					});
				} catch (error) {
					console.error('打印机初始化失败:', error);
					uni.showToast({
						title: '打印机初始化失败',
						icon: 'error',
						duration: 2000
					});
				}
			},

			async closePrinter() {
				if (!printer || !this.isPrinterInitialized) {
					uni.showToast({
						title: '打印机未初始化',
						icon: 'error',
						duration: 2000
					});
					return;
				}

				try {
					await printer.closePrinter();
					this.isPrinterInitialized = false;
					uni.showToast({
						title: '打印机已关闭',
						icon: 'success',
						duration: 2000
					});
				} catch (error) {
					console.error('关闭打印机失败:', error);
					uni.showToast({
						title: '关闭打印机失败',
						icon: 'error',
						duration: 2000
					});
				}
			},

			async printerLabel() {
				if (!this.checkPrinterStatus()) return;

				try {
					console.log('开始测试打印...');
					
					await printer.setFontSize({ fontSize: 0 });
					await printer.setTextBold({ bold: true });
					
					await printer.printText({ content: '垃圾收运小票凭证' });
					await printer.printLine({ line_length: 1 });
					await printer.printText({ content: 'Test Print' });
					await printer.printLine({ line_length: 1 });
					
					await printer.printQR({
						text: 'Test QR Code',
						height: 400,
						offset: 1
					});
					
					await printer.printLine({ line_length: 2 });
					await printer.start();
					
					console.log('测试打印完成');
				} catch (error) {
					console.error('测试打印失败:', error);
					uni.showToast({
						title: '测试打印失败',
						icon: 'error',
						duration: 2000
					});
				}
			},

			async printerText() {
				if (!this.checkPrinterStatus()) return;

				try {
					console.log('开始打印小票...');
					
					await printer.setFontSize({ fontSize: 0 });
					await printer.setTextBold({ bold: true });
					
					await printer.printText({ content: '这是一张测试小票' });
					await printer.setTextBold({ bold: false });
					await printer.printLine({ line_length: 1 });
					
					const items = ['商品', '数量', '单价'];
					for (const item of items) {
						await printer.printText({ content: item });
						await printer.printLine({ line_length: 1 });
					}
					
					await printer.printBarcode({
						text: '123456',
						height: 80,
						barcodeType: 73
					});
					
					await printer.printLine({ line_length: 1 });
					await printer.printQR({ text: '1234456' });
					await printer.printLine({ line_length: 2 });
					await printer.start();
					
					console.log('小票打印完成');
				} catch (error) {
					console.error('小票打印失败:', error);
					uni.showToast({
						title: '小票打印失败',
						icon: 'error',
						duration: 2000
					});
				}
			},

			async printLabel() {
				if (!this.checkPrinterStatus()) return;

				try {
					await printer.printLine({ line_length: 5 });
					await printer.printGoToNextMark();
					console.log('标签打印完成');
				} catch (error) {
					console.error('标签打印失败:', error);
					uni.showToast({
						title: '标签打印失败',
						icon: 'error',
						duration: 2000
					});
				}
			},

			checkPrinterStatus() {
				if (!printer) {
					uni.showToast({
						title: '打印机插件未加载',
						icon: 'error',
						duration: 2000
					});
					return false;
				}

				if (!this.isPrinterInitialized) {
					uni.showToast({
						title: '请先初始化打印机',
						icon: 'none',
						duration: 2000
					});
					return false;
				}

				return true;
			}
		}
	};
</script>

<style>
.button {
	margin: 10px;
	padding: 10px;
}
</style>
